#!/usr/bin/perl -w

use Parse::RecDescent;

my $grammar = q{
  DiffOutput:Command(s) /\Z/

  Command:
     LineNum 'a' LineRange RightLine(s)
       { print "$item[3]d$item[1]\n";
	 print map {s/>/</; $_} @{$item[4]};
       }
   | LineRange 'd' LineNum LeftLine(s)
       { print "$item[3]a$item[1]\n";
	 print map {s/</>/; $_} @{$item[4]};
       }
   | LineRange 'c' LineRange
     LeftLine(s) "---\n" RightLine(s)
       { print "$item[3]c$item[1]\n";
	 print map {s/>/</; $_} @{$item[6]};
	 print $item[5];
	 print map {s/</>/; $_} @{$item[4]};}
   | <error: Invalid diff(1) command!>

LineRange:
   LineNum ',' LineNum
     { join '',@item[1..3] }
   | LineNum

LineNum: /\d+/
LeftLine: /<.*\n/
RightLine: />.*\n/
};
my $parser = new Parse::RecDescent($grammar);

undef $/;
my $text = <STDIN>;

$parser->DiffOutput($text);